 window.onload = function(){
    var width ;     //body长度
    var index = 0;  //首屏轮播序号
    /*
    *确定body中相关元素大小
    */
    function makeSize(){
        var page = 3;
        width = document.body.clientWidth < 1120 ? 1120 : document.body.clientWidth;
        var imgs = document.querySelectorAll(".carousel img");
        for (var i = imgs.length - 1; i >= 0; i--) {
            imgs[i].style.width     = width+"px";
            imgs[i].style.height    = width/3 + "px";
        }
        var carousel = document.querySelectorAll(".carousel")[0];
        carousel.style.width = width + "px";
        carousel.style.height = width*0.3 + 135 + "px";
        var container = document.querySelectorAll(".carousel .container")[0];
        container.style.left = -width*(index+1) + "px";
        container.style.width = width*(page+2) + "px";
    }
    makeSize();
    window.onresize = function(){
        makeSize();
    };
    /*
    *首页轮播图
    */
    (function(){
        var container = document.getElementsByClassName('container')[0];
        var dot = document.getElementsByClassName('dot');
        var dotContent = document.getElementsByClassName('dots')[0];
        var page = 3;  
        var time =300;    //位移总的时间
        var interval = 10; //位移间隔时间
        var isMoving = false;
        function showButton(i){
            dot[i].classList.add('select');
        }
        function hideButton(i){
            dot[i].classList.remove('select');
        }
        function move(offset,callback){
            isMoving = true;
            var newLeft = container.offsetLeft + offset;    //新的left
            var speed = offset/(time/interval);
            //移动
            function go(){
                if((speed < 0 && container.offsetLeft > newLeft) || (speed > 0 && container.offsetLeft < newLeft)){
                    container.style.left = container.offsetLeft + speed + 'px';
                    if((speed < 0 && container.offsetLeft < newLeft) || (speed > 0 && container.offsetLeft > newLeft)){
                        container.style.left = newLeft + 'px';
                    }
                    setTimeout(go,interval);
                }else{
                    isMoving = false;
                    //判断是否到替换图
                    if(container.offsetLeft > -width){
                        container.style.left = -width*page + 'px';
                    }else if(container.offsetLeft < -width*page){
                        container.style.left = -width + 'px';
                    }
                }
            }
            go();
            //回调，切换圆点
            var direction = offset > 0 ?  -1 : 1 ;
            callback(direction);
        }
        //点击箭头切换
        var left = document.getElementsByClassName('left')[0];
        left.onclick = function(){
            if(isMoving){
                return;
            }
            move(width,function(direction){
                //圆点
                hideButton(index);
                index = ((index + direction)%page) < 0? ((index + direction)%page+page) : ((index + direction)%page) ;
                showButton(index);
            });
        };
        var right = document.getElementsByClassName('right')[0];
        right.onclick = function(){
            if(isMoving){
                return;
            }
            move(-width,function(direction){
                //圆点
                hideButton(index);
                index = ((index + direction)%page) < 0? ((index + direction)%page+page) : ((index + direction)%page) ;
                showButton(index);
            });
        };
        //点击圆点切换
        dotContent.onclick = function(e){
            if(e.target.tagName.toLowerCase() == 'span'){
                newIndex = parseInt(e.target.id) - 1;
                if(newIndex == index){
                    return;
                }
                //新偏移量
                container.style.left = parseInt(e.target.id)*(-width) + 'px';
                hideButton(index);
                index = newIndex;
                showButton(index);
            }
        }
        //定时切换
        setInterval(function(){
            if(isMoving){
                return;
            }
            move(-width,function(direction){
                //圆点
                hideButton(index);
                index = ((index + direction)%page) < 0? ((index + direction)%page+page) : ((index + direction)%page) ;
                showButton(index);
            });
        },5000);

    })();
    /*
    *综合新闻通知公告变换
    */
    (function(){
        var resultCoop = document.querySelector(".notice-news .hd");
        resultCoop.onmouseover = function(e){
            if(e.target.tagName.toLowerCase() == 'li'){
                var name = e.target.id;
                var otherName = (name == "notice") ? "news" : "notice";
                var ele = document.getElementsByClassName(name)[0];
                var otherEle = document.getElementsByClassName(otherName)[0];
                ele.classList.remove("hide");
                ele.classList.add("show");
                otherEle.classList.remove("show");
                otherEle.classList.add("hide");
                document.getElementById(name).classList.add("on");
                document.getElementById(otherName).classList.remove("on");
            }
        }
    });
    /*
    *成果展示合作动态变换
    */
    (function(){
        var resultCoop = document.querySelector(".result-coop .hd");
        resultCoop.onmouseover = function(e){
            if(e.target.tagName.toLowerCase() == 'li'){
                var name = e.target.id;
                var otherName = (name == "result") ? "coop" : "result";
                var ele = document.getElementsByClassName(name)[0];
                var otherEle = document.getElementsByClassName(otherName)[0];
                ele.classList.remove("hide");
                ele.classList.add("show");
                otherEle.classList.remove("show");
                otherEle.classList.add("hide");
                document.getElementById(name).classList.add("on");
                document.getElementById(otherName).classList.remove("on");
            }
        }
    });
    /*
    *合作剪影轮播图
    */
    (function(){
        var width = 252;  //图片+图片间隔长度
        var imgs = 4;   //图片数量
        var time = 20;  //移动的间隔
        var speed = 1; //每次向左移动的像素
        var imgsContent = document.getElementsByClassName("imgs-cont")[0];
        var interval;
        
        function go(){
            var offset = imgsContent.offsetLeft;
            imgsContent.style.left = offset - speed + 'px';
            offset = imgsContent.offsetLeft;
            if (offset < -width) {
                imgsContent.appendChild(imgsContent.firstElementChild);
                imgsContent.style.left = offset+width+'px';
            }
        }

        imgsContent.onmouseover = function(){
            clearInterval(interval);
        }
        imgsContent.onmouseout = function(){
            interval = setInterval(go,time);
        }

        interval = setInterval(go,time);
    })();
    (function(){
        var width = 252;  //图片+图片间隔长度
        var imgs = 4;   //图片数量
        var time = 20;  //移动的间隔
        var speed = 1; //每次向左移动的像素
        var imgsContent = document.getElementsByClassName("imgs-cont")[1];
        var interval;
        
        function go(){
            var offset = imgsContent.offsetLeft;
            imgsContent.style.left = offset - speed + 'px';
            offset = imgsContent.offsetLeft;
            if (offset < -width) {
                imgsContent.appendChild(imgsContent.firstElementChild);
                imgsContent.style.left = offset+width+'px';
            }
        }

        imgsContent.onmouseover = function(){
            clearInterval(interval);
        }
        imgsContent.onmouseout = function(){
            interval = setInterval(go,time);
        }

        interval = setInterval(go,time);
    })();
    /*
    *图片新闻区
    */
    (function(){
        var little = document.querySelector(".img-news .little");
        var bigImg = document.getElementsByClassName("big-img")[0];
        var nextBig = document.getElementsByClassName("big-img")[1];
        var container = document.querySelector(".img-news .container");
        var imgs = document.querySelectorAll(".img-news .little img");
        var speed = 2;  //移动的像素
        var time = 5; //每移动一次的间隔
        var interval = 1000; //每次移动照片的事件间隔
        var width = 380;
        var curent = 0; //当前图片
        var curentBig = 'big';  //当前展示的大图（big or nextbig）
        var interval;
        var onMoving = false;

        for(var i=0;i<imgs.length;i++){
            imgs[i].setAttribute("index",i);
        }
        bigImg.src = imgs[0].src;
        bigImg.parentElement.href = "/s/xdhz/content/?type=article&contentUrl=" + imgs[0].url;
        bigImg.title = imgs[0].title;
        nextBig.src = imgs[1].src;
        nextBig.parentElement.href = "/s/xdhz/content/?type=article&contentUrl=" + imgs[1].url;
        nextBig.parentElement.title = imgs[1].title;
        little.onclick = function(e){
            if(onMoving){
                return;
            }
            /**
             * 在这里也添加onMoving = true;
             */
            onMoving = true;
            if(e.target.tagName.toLowerCase() == "img"){
                curent = parseInt(e.target.getAttribute("index"));   //图片索引
                var src = e.target.src; //点击的图片
                var title = e.target.title; //图片标题
                var href = "/s/xdhz/content/?type=article&contentUrl=" + e.target.getAttribute("url"); //图片指向的链接
                var parent = bigImg.parentElement;
                var nextParent = nextBig.parentElement;
                var nextImg = imgs[(curent+1)%8];

                var nextSrc = nextImg.src;
                var nextTitle = nextImg.title;
                var nextHref = "/s/xdhz/content/?type=article&contentUrl=" + nextImg.getAttribute("url");
                if(curentBig === 'big'){
                    //大图
                    bigImg.src = src;
                    bigImg.title = title;
                    parent.href = href;
                    //下一张大图
                    nextBig.src = nextSrc;
                    nextBig.title = nextTitle;
                    nextParent.href = nextHref;
                }else{
                    //大图
                    bigImg.src = nextSrc;
                    bigImg.title = nextTitle;
                    parent.href = nextHref;
                    //下一张大图
                    nextBig.src = src;
                    nextBig.title = title;
                    nextParent.href = href;
                }
            }
             /**
             * 在这里也添加onMoving = false;
             */
            onMoving = false;
        }
        function go(){
            var left = container.offsetLeft;
            if(width+left>speed){
                container.style.left = left - speed + 'px';
                setTimeout(go,time);
            }else{
                //最后一次移动
                container.style.left = -width + 'px';   //一张图片播完
                curent = (curent+1)%8;
                var nextImg = imgs[(curent+1)%8];
                container.firstElementChild.setAttribute("href",nextImg.getAttribute("url"));
                container.firstElementChild.firstElementChild.src = nextImg.src;
                container.firstElementChild.firstElementChild.title = nextImg.title;
                container.appendChild(container.firstElementChild);
                container.style.left = "0px";
                onMoving = false;
                curentBig = curentBig==='big' ? 'nextBig' : 'big';
            }
        }
        setInterval(function(){
            if(!onMoving){
                onMoving = true;
                go();
            }
        },4000);
    });
    /**
     *  修改a标签href
    */
    $(function(){
        $("a[url]").each(
            function(){
                $(this).attr("href","/s/xdhz/content/?type=article&contentUrl="+$(this).attr("url"));
            }
        );
        /*var as = document.querySelectorAll("a[url]");
        for(var i=0;i<as.length;i++){
            if(as[i].url){
                as[i].href = "/s/xdhz/content/?type=article&contentUrl=" + as[i].url;
            }else{
                as[i].href = "/s/xdhz/content/?type=article&contentUrl=" + as[i].getAttribute("url");
            }
            
        }*/
    });
    
    /**
     * hashchange事件
     */
    window.onhashchange = hashchange;
    function hashchange(){
        /**
         *  #1 有主页大图、合作项目成果展示与上面不对齐
            #2 无主页大图
            #3 合作项目成果展示与上面对齐
            #4 改变理念框配色2
            #5 改变理念框配色3
            #6 改变理念框样式2
            #7 改变整体配色为绿色
         */
        console.log(this.location.hash.substring(1));
        switch(this.location.hash.substring(1)){
            case '1':$(".carousel").removeClass("hide");break;
            case '2':$(".carousel").addClass("hide");break;
            case '3':
                $(".result").eq(1).removeClass("hide");
                $(".project").eq(1).removeClass("hide");
                $(".result").eq(0).addClass("hide");
                $(".project").eq(0).addClass("hide");
                break;
            case '4':
                $(".carousel").removeClass("hide");
                $(".concept").removeClass("color1 color2 color3").addClass("color2");
                break;
            case '5':
                $(".carousel").removeClass("hide");
                $(".concept").removeClass("color1 color2 color3").addClass("color3");
                break;
            case '6':
                $(".carousel").removeClass("hide");
                $(".concept").removeClass("display1 display2").addClass("display2");
                break;
            case '7':
                $(".content").addClass("green");
                break;
            case '8':
                $(".switchState").removeClass("switchState");
                $(".chongqing-action").addClass("hide");
                $(".difanglianxian").addClass("hide");
                $(".zhengcefagui").eq(0).addClass("hide");
                $(".zhengcefagui").eq(1).removeClass("hide");
                
                break
        }
        
    }
    hashchange();
}
